#include "ui_status.h"
#include "esp_log.h"
#include "lvgl.h"
#include "esp_lvgl_port.h"

static const char *TAG = "ui_status";

static lv_obj_t *status_cont = NULL;
static lv_obj_t *lbl_wifi = NULL;
static lv_obj_t *lbl_bt   = NULL;

static void create_status_controls(lv_obj_t *parent)
{
    if (!parent) parent = lv_scr_act();

    if (!status_cont) {
        status_cont = lv_obj_create(parent);
        lv_obj_set_width(status_cont, lv_pct(100));
        lv_obj_set_height(status_cont, 30);
        lv_obj_clear_flag(status_cont, LV_OBJ_FLAG_SCROLLABLE);
        lv_obj_set_style_pad_left(status_cont, 8, 0);
        lv_obj_set_style_pad_right(status_cont, 8, 0);
        lv_obj_set_style_pad_top(status_cont, 0, 0);
        lv_obj_set_style_pad_bottom(status_cont, 0, 0);
        lv_obj_set_style_bg_opa(status_cont, LV_OPA_TRANSP, 0);
        lv_obj_set_layout(status_cont, LV_LAYOUT_FLEX);
        lv_obj_set_flex_flow(status_cont, LV_FLEX_FLOW_ROW);
        lv_obj_set_flex_align(status_cont, LV_FLEX_ALIGN_END, LV_FLEX_ALIGN_CENTER, LV_FLEX_ALIGN_END);
        lv_obj_t *spacer = lv_obj_create(status_cont);
        lv_obj_remove_style_all(spacer);
        lv_obj_set_size(spacer, LV_SIZE_CONTENT, LV_SIZE_CONTENT);
        lv_obj_set_style_flex_grow(spacer, 1, 0);
    }

    if (!lbl_wifi) {
        lbl_wifi = lv_label_create(status_cont);
        lv_label_set_text(lbl_wifi, "WiFi: -");
        lv_obj_set_size(lbl_wifi, LV_SIZE_CONTENT, LV_SIZE_CONTENT);
        lv_label_set_long_mode(lbl_wifi, LV_LABEL_LONG_CLIP);
        lv_obj_set_style_text_align(lbl_wifi, LV_TEXT_ALIGN_RIGHT, 0);
        lv_obj_set_style_pad_top(lbl_wifi, 0, 0);
        lv_obj_set_style_pad_bottom(lbl_wifi, 0, 0);
    }

    if (!lbl_bt) {
        lbl_bt = lv_label_create(status_cont);
        lv_label_set_text(lbl_bt, "BT: -");
        lv_obj_set_size(lbl_bt, LV_SIZE_CONTENT, LV_SIZE_CONTENT);
        lv_label_set_long_mode(lbl_bt, LV_LABEL_LONG_CLIP);
        lv_obj_set_style_text_align(lbl_bt, LV_TEXT_ALIGN_RIGHT, 0);
        lv_obj_set_style_pad_top(lbl_bt, 0, 0);
        lv_obj_set_style_pad_bottom(lbl_bt, 0, 0);
    }
}

void ui_status_init(lv_obj_t *parent)
{
    create_status_controls(parent);
}

void ui_status_set_wifi(bool connected)
{
    if (!lbl_wifi) {
        ESP_LOGW(TAG, "lbl_wifi not ready");
        return;
    }
    if (lvgl_port_lock(0)) {
#ifdef LV_SYMBOL_WIFI
        if (connected) lv_label_set_text(lbl_wifi, LV_SYMBOL_WIFI " ON");
        else lv_label_set_text(lbl_wifi, LV_SYMBOL_WIFI " OFF");
#else
        if (connected) lv_label_set_text(lbl_wifi, "WiFi: ON");
        else lv_label_set_text(lbl_wifi, "WiFi: OFF");
#endif
        if (connected) lv_obj_set_style_text_color(lbl_wifi, lv_color_hex(0x00cc00), 0);
        else lv_obj_set_style_text_color(lbl_wifi, lv_color_hex(0xcc0000), 0);
        lvgl_port_unlock();
    } else {
#ifdef LV_SYMBOL_WIFI
        if (connected) lv_label_set_text(lbl_wifi, LV_SYMBOL_WIFI " ON");
        else lv_label_set_text(lbl_wifi, LV_SYMBOL_WIFI " OFF");
#else
        if (connected) lv_label_set_text(lbl_wifi, "WiFi: ON");
        else lv_label_set_text(lbl_wifi, "WiFi: OFF");
#endif
        if (connected) lv_obj_set_style_text_color(lbl_wifi, lv_color_hex(0x00cc00), 0);
        else lv_obj_set_style_text_color(lbl_wifi, lv_color_hex(0xcc0000), 0);
    }
}

void ui_status_set_bt(bool connected)
{
    if (!lbl_bt) {
        ESP_LOGW(TAG, "lbl_bt not ready");
        return;
    }
    if (lvgl_port_lock(0)) {
#ifdef LV_SYMBOL_BLUETOOTH
        if (connected) lv_label_set_text(lbl_bt, LV_SYMBOL_BLUETOOTH " ON");
        else lv_label_set_text(lbl_bt, LV_SYMBOL_BLUETOOTH " OFF");
#else
        if (connected) lv_label_set_text(lbl_bt, connected ? "BT: ON" : "BT: OFF");
#endif
        if (connected) lv_obj_set_style_text_color(lbl_bt, lv_color_hex(0x00cc00), 0);
        else lv_obj_set_style_text_color(lbl_bt, lv_color_hex(0xcc0000), 0);
        lvgl_port_unlock();
    } else {
#ifdef LV_SYMBOL_BLUETOOTH
        if (connected) lv_label_set_text(lbl_bt, LV_SYMBOL_BLUETOOTH " ON");
        else lv_label_set_text(lbl_bt, LV_SYMBOL_BLUETOOTH " OFF");
#else
        if (connected) lv_label_set_text(lbl_bt, connected ? "BT: ON" : "BT: OFF");
#endif
        if (connected) lv_obj_set_style_text_color(lbl_bt, lv_color_hex(0x00cc00), 0);
        else lv_obj_set_style_text_color(lbl_bt, lv_color_hex(0xcc0000), 0);
    }
}